const mixins = require('../../mixins/forum')
const common = require('../../mixins/common');
const {
  addForums
} = require('../../api/forum');
const app = getApp()
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    scale: 14,
    screenRatio: 1,
    coverMapshow: false,
    type: 'forums',
    mapStatus: !0,
    posts: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getLocation({
      success: function (res) {
        that.getLocationPost(res.longitude, res.latitude);
        that.setData({
          curCenter: [res.longitude, res.latitude],
          longitude: res.longitude,
          lng: res.longitude,
          lat: res.latitude,
          latitude: res.latitude,
        })
      },
    })
  },

  /** 切换模式 */
  switchStatus: function (t) {
    var a = this;
    a.data.mapStatus = !a.data.mapStatus, a.setData({
      mapStatus: a.data.mapStatus,
      mapHidden: !a.data.mapStatus
    })
  },

  /** 点击地图 */
  tapmap: function () {
    this.setData({
      coverMapshow: false,
    })
  },

  /** 点击地图上的mark  */
  markertap: function (e) {
    let id = e.detail.markerId;
    console.log(e);
    this.setData({
      id: id,
      coverMapshow: !this.data.coverMapshow,
    })
    this.getForumDetails();
  },

  /** 跳转详情  */
  gotoDetail: function (e) {
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?type=forums&id=' + id,
    })
  },
  /**
   * 视频播放
   */

  videoplay: function (url) {
    let that = this;
    wx.request({
      url: 'https://mall.guqule.com/mp/video.php',
      data: {
        url: url,
      },
      method: 'get',
      success: function (res) {
        console.log(res);
        if (res.data.status == 200) {
          that.setData({
            playurl: res.data.url
          })
        }
      }
    })

  },
  /** 返回上一级  */
  back: function () {
    wx.navigateBack({})
  },

  /**重置数据  */
  resetDiscoveryData: function () {
    this.setData({
      markers: [],
      includePoints: [],
      curDiscovery: 0,
      discoveryTotal: 0,
      userList: []
    });
  },
  /**放大地图 */
  zoomIn: function (e) {
    var t = this.data.scale + 1;
    t > 19 ? wx.showToast({
      title: "已经扩大到极限",
      icon: "none"
    }) : this.setData({
      scale: t
    });
  },
  /**缩小地图 */
  zoomOut: function (e) {
    var t = this.data.scale - 1;
    t < 14 ? wx.showToast({
      title: "已经缩放到极限",
      icon: "none"
    }) : this.setData({
      scale: t
    });
  },


  getUserAvatarBg: function (t) {
    var e = "../../image/pos_";
    return e += "0" == t ? "male.png" : "1" == t ? "female.png" : "unknown.png";
  },

  getMarkerTip: function () {
    this.data.screenRatio;
    var t = ["hi", "Here", "Hello", "咦？", "嗨", "啊", "唔~", "你好呀", "你也在？", "恩？", "你在偷看我？", "哇！", "被你发现了", "yeah~", "哟！", "skr~skr~"],
      e = t.length;
    return {
      content: t[Math.floor(Math.random() * e)],
      color: "#FFFFFF",
      fontSize: 12,
      borderRadius: 6,
      bgColor: "#666",
      padding: 6,
      display: "ALWAYS"
    };
  },
  /**获取距离 */
  getDistance: function (t, e, a, i) {
    var n = this.getRad(t),
      r = this.getRad(a),
      s = n - r,
      o = this.getRad(e) - this.getRad(i),
      c = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(s / 2), 2) + Math.cos(n) * Math.cos(r) * Math.pow(Math.sin(o / 2), 2)));
    return c *= 6378137, Math.round(1e4 * c) / 1e4 * .001;
  },
  /** 获取用户头像 */
  getUserAvatar: function (t) {
    for (var e = this, a = 0; a < t.length; a++) ! function (a) {
      var i = t[a],
        n = i.author.avatar,
        r = i.author.status,
        s = setTimeout(function () {
          wx.getImageInfo({
            src: n,
            success: function (t) {
              var i = wx.createCanvasContext("myCanvas");
              i.drawImage(e.getUserAvatarBg(r), 0, 0, 120, 160), i.save(), i.arc(60, 60, 48, 0, 2 * Math.PI),
                i.clip();
              var n = t.path;
              n.indexOf("tmp") < 0 && (n = "../../" + n), i.drawImage(n, 12, 12, 96, 96), i.restore(),
                i.beginPath(), i.arc(60, 60, 48, 0, 2 * Math.PI), i.setStrokeStyle("#ffffff"), i.setLineWidth(2),
                i.stroke(), i.beginPath(), i.arc(102, 102, 18, 0, 2 * Math.PI), i.setFillStyle("#F2C858"),
                i.fill(), i.setFillStyle("#8B572A"), i.setFontSize(16), i.draw(!1, function () {
                  wx.canvasToTempFilePath({
                    width: 120,
                    height: 160,
                    destWidth: 120,
                    destHeight: 160,
                    quality: 1,
                    canvasId: "myCanvas",
                    success: function (t) {
                      var i = e.data.markers;
                      i && i[a].iconPath ? (i[a].iconPath = t.tempFilePath, e.setData({
                        markers: i
                      })) : clearTimeout(s);
                    },
                    fail: function (t) {

                    }
                  });
                });
            },
            fail: function (t) {

            }
          });
        }, 500 * (a + 1));
    }(a);
  },
  getRad: function (t) {
    return t * Math.PI / 180;
  },
 



  /**
   * 范围改变
   */

  regionchange: function (e) {

    if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
      var that = this;
      this.mapCtx = wx.createMapContext("map");
      this.mapCtx.getCenterLocation({
        type: 'gcj02',
        success: function (res) {
          that.setData({
            markers: [],
            latitude: res.latitude,
            longitude: res.longitude,
            circles: [{
              latitude: res.latitude,
              longitude: res.longitude,
              color: '#FF00DD00',
              fillColor: '#d1edff88',
              radius: 5000, //定位点半径
              strokeWidth: 1
            }]
          })
          that.getLocationPost(that.data.longitude, that.data.latitude)
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 打开地图导航
   */

  openmap: function (e) {
    var lng = parseFloat(e.currentTarget.dataset.lng);
    var lat = parseFloat(e.currentTarget.dataset.lat);
    var location = e.currentTarget.dataset.location;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      location: location
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}
mixins(options)
common(options)
Page(options)